package org.micode.common.validater;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PhoneValidater {

    public static ValidateResult validate(String phone) {
        if (StringUtils.isBlank(phone)) {
            return new ValidateResult(false, "电话号码不能为空");
        }

        String regex = "^[+,0-9][0-9]{5,20}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(phone);
        if (!m.matches()) {
            return new ValidateResult(false, "电话号码不正确");
        }

        return new ValidateResult(true, "");
    }

    public static ValidateResult validateChsMobilePhone(String phone) {
        if (StringUtils.isBlank(phone)) {
            return new ValidateResult(false, "电话号码不能为空");
        }

        String regex = "^((13[0-9])|(14[5,7,9])|(15([0-3]|[5-9]))|(166)|(17[0,1,3,5,6,7,8])|(18[0-9])|(19[8|9]))\\d{8}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(phone);
        if (!m.matches()) {
            return new ValidateResult(false, "电话号码不正确");
        }

        return new ValidateResult(true, "");
    }

    private static void test(String phone) {
        System.out.println(phone + " : " + validate(phone));
    }

    public static void main(String[] args) {
        test("+1211");
        test("09811");
        test("111221");
        test("2121321");
        test("3121321");
        test("4121321");
        test("52121321");
        test("62121321");
        test("72121321");
        test("8212 1321");
        test("9212r1321");
        test("a2121321");
        test(" 2121321");
    }

}
